/**
 * 添加用户
 * @author williamJM
 * date 2018/04/17 19:45
 */

"use strict";

define(['app', "config/pathConfig", "layer"], function (app, pathConfig, layer) {
    app.registerController('ctrl.userManager.addUser', function ($scope, $http) {

        $http.get(pathConfig.backstagePath.getPath() + "/department")
            .then(function successCallback(response) {
                $scope.post = response.data.data;
                if ($scope.post !== null) {
                    $scope.user.post = $scope.post[0].postId + '';
                }
            }, function errorCallback(response) {

            });
        $scope.user = {};

        var submit = function (user) {
            return new Promise(function (success, error) {
                $http.post(pathConfig.backstagePath.getPath() + "/userManager", user)
                    .then(function successCallback(response) {
                        success(response.data);
                    }, function errorCallback(resonse) {
                        error(resonse);
                    })
            })
        };


        $scope.add = function () {

            layer.confirm('确定要保存当前页面的信息吗？', {
                icon: 3,
                title: '保存提示',
                btn: ['确定', '取消']
            }, function () {
                submit($scope.user).then(function (callback) {
                    if (callback.status === 200) {
                        layer.msg('保存成功！', {icon: 1});
                    } else if (callback.status === 500) {
                        layer.msg("服务器错误！", {icon: 2});
                    }
                }, function () {
                    layer.msg("服务器错误！", {icon: 2});
                });

            });
        }


    });
});
